/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgConfig.cpp : implementation file

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgConfig.h"

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgConfig, CRgnDialog)

DlgConfig::DlgConfig(CWnd* pParent /*=NULL*/) : CRgnDialog(DlgConfig::IDD, pParent) 
{
	skinIn = NULL;
	retVal = false;
	config.Initialize();
}

DlgConfig::~DlgConfig()
{
}

//---------------------------------------------------------------------------

void DlgConfig::DoDataExchange(CDataExchange* pDX)
{
	CRgnDialog::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_LBL_CUSK, lblCusk);
	DDX_Control(pDX, IDC_EDT_CUSK, edtCusk);
	DDX_Control(pDX, IDC_CHK_BOC, chkBoc);
	DDX_Control(pDX, IDC_LBL_NOTIF1, lblNotif1);
	DDX_Control(pDX, IDC_EDT_NOTIF1, edtNotif1);
	DDX_Control(pDX, IDC_LBL_PACK1, lblPack1);
	DDX_Control(pDX, IDC_LBL_NOTIF2, lblNotif2);
	DDX_Control(pDX, IDC_EDT_NOTIF2, edtNotif2);
	DDX_Control(pDX, IDC_LBL_PACK2, lblPack2);
	DDX_Control(pDX, IDC_LBL_NOTIF3, lblNotif3);
	DDX_Control(pDX, IDC_EDT_NOTIF3, edtNotif3);
	DDX_Control(pDX, IDC_LBL_PACK3, lblPack3);
	DDX_Control(pDX, IDC_LBL_NOTIF4, lblNotif4);
	DDX_Control(pDX, IDC_EDT_NOTIF4, edtNotif4);
	DDX_Control(pDX, IDC_LBL_PACK4, lblPack4);
	DDX_Control(pDX, IDC_BTN_OK, btnOk);
	DDX_Control(pDX, IDC_BTN_CANCEL, btnCancel);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgConfig, CRgnDialog)
	ON_BN_CLICKED(IDC_BTN_OK, &DlgConfig::OnBnClickedBtnOk)
	ON_BN_CLICKED(IDC_BTN_CANCEL, &DlgConfig::OnBnClickedBtnCancel)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

BOOL DlgConfig::OnInitDialog()
{
	CRgnDialog::OnInitDialog();

	SetProperties(skinIn, false, _T("Config"));

	if(skin!=NULL){
		lblCusk.textColor = skin->textColor;
		chkBoc.textColor  = skin->textColor;

		lblNotif1.textColor = skin->textColor;
		lblPack1.textColor = skin->textColor;
		lblNotif2.textColor = skin->textColor;
		lblPack2.textColor = skin->textColor;
		lblNotif3.textColor = skin->textColor;
		lblPack3.textColor = skin->textColor;
		lblNotif4.textColor = skin->textColor;
		lblPack4.textColor = skin->textColor;
	}

	WorkCfgToData();

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
}

//---------------------------------------------------------------------------

void DlgConfig::OnOK()
{
	// CRgnDialog::OnOK();
}

void DlgConfig::OnCancel()
{
	// CRgnDialog::OnCancel();
}

//---------------------------------------------------------------------------

void DlgConfig::OnBnClickedBtnOk()
{
	UpdateData(TRUE);
	WorkCfgFromData();

	retVal = true;
	EndDialog(IDOK);
}

void DlgConfig::OnBnClickedBtnCancel()
{
	retVal = false;
	EndDialog(IDABORT);
}

//---------------------------------------------------------------------------

void DlgConfig::WorkCfgToData(void)
{
	CString str;

	edtCusk.SetWindowText(config.theSkin);
	chkBoc.SetCheck(config.completionBeep ? BST_CHECKED : BST_UNCHECKED);

	str.Format(_T("%d"), config.notifTcp);
	edtNotif1.SetWindowText(str);
	str.Format(_T("%d"), config.notifUdp);
	edtNotif2.SetWindowText(str);
	str.Format(_T("%d"), config.notifPing);
	edtNotif3.SetWindowText(str);
	str.Format(_T("%d"), config.notifNetBios);
	edtNotif4.SetWindowText(str);
}

void DlgConfig::WorkCfgFromData(void)
{
	CString str;
	int val;

	//config.theSkin = 
	config.completionBeep = (chkBoc.GetCheck()==BST_CHECKED) ? true : false;
	
	edtNotif1.GetWindowText(str);
	val = _tstoi(str);
	if(val<NOTIF_TCP_MIN) val = NOTIF_TCP_MIN;
	if(val>NOTIF_TCP_MAX) val = NOTIF_TCP_MAX;
	config.notifTcp = val;

	edtNotif2.GetWindowText(str);
	val = _tstoi(str);
	if(val<NOTIF_UDP_MIN) val = NOTIF_UDP_MIN;
	if(val>NOTIF_UDP_MAX) val = NOTIF_UDP_MAX;
	config.notifUdp = val;

	edtNotif3.GetWindowText(str);
	val = _tstoi(str);
	if(val<NOTIF_ICMP_MIN) val = NOTIF_ICMP_MIN;
	if(val>NOTIF_ICMP_MAX) val = NOTIF_ICMP_MAX;
	config.notifPing = val;

	edtNotif4.GetWindowText(str);
	val = _tstoi(str);
	if(val<NOTIF_L4_MIN) val = NOTIF_L4_MIN;
	if(val>NOTIF_L4_MAX) val = NOTIF_L4_MAX;
	config.notifNetBios = val;
}

//---------------------------------------------------------------------------

void DlgConfig::ResizeChilds(void)
{
	int bw, bh;		///< size of button controls
	int lw1, lw2;	///< width of label controls
	int ew, eh;		///< size of edit and combobox controls
	int tw, th;		///< total required width and height
	int cw, ch;		///< client region's width and height
	int mx, my;		///< left and top space for displaying the objects in the center of the window
	int dd;			///< space between objects
	RECT rect;
	int pos1, pos2, pos3, posy;

	dd = 2;

	btnOk.GetWindowRect(&rect);
	bw = rect.right - rect.left;
	bh = rect.bottom - rect.top;

	lblNotif1.GetWindowRect(&rect);
	lw1 = rect.right - rect.left;
	lblPack1.GetWindowRect(&rect);
	lw2 = rect.right - rect.left;
	edtNotif1.GetWindowRect(&rect);
	ew = rect.right - rect.left;
	eh = rect.bottom - rect.top;
	
	tw = lw1 + ew + lw2 + 2*dd;
	th = 6*eh + bh + 6*dd;

	cw = clientRect.right - clientRect.left;
	ch = clientRect.bottom - clientRect.top;

	mx = cw - tw; mx /= 2;
	my = ch - th; my /= 2;

	posy = clientRect.top + my;
	pos1 = clientRect.left + mx;
	pos2 = pos1 + lw1 + dd;
	pos3 = pos2 + ew + dd;

	lblCusk.MoveWindow(pos1, posy, lw1, eh, TRUE);
	edtCusk.MoveWindow(pos2, posy, ew + dd + lw2, eh, TRUE);

	posy += (dd + eh);
	chkBoc.MoveWindow(pos2, posy, ew + dd + lw2, eh, TRUE);

	posy += (dd + eh);
	lblNotif1.MoveWindow(pos1, posy, lw1, eh, TRUE);
	edtNotif1.MoveWindow(pos2, posy,  ew, eh, TRUE);
	lblPack1.MoveWindow( pos3, posy, lw2, eh, TRUE);

	posy += (dd + eh);
	lblNotif2.MoveWindow(pos1, posy, lw1, eh, TRUE);
	edtNotif2.MoveWindow(pos2, posy,  ew, eh, TRUE);
	lblPack2.MoveWindow( pos3, posy, lw2, eh, TRUE);

	posy += (dd + eh);
	lblNotif3.MoveWindow(pos1, posy, lw1, eh, TRUE);
	edtNotif3.MoveWindow(pos2, posy,  ew, eh, TRUE);
	lblPack3.MoveWindow( pos3, posy, lw2, eh, TRUE);

	posy += (dd + eh);
	lblNotif4.MoveWindow(pos1, posy, lw1, eh, TRUE);
	edtNotif4.MoveWindow(pos2, posy,  ew, eh, TRUE);
	lblPack4.MoveWindow( pos3, posy, lw2, eh, TRUE);

	posy += (dd + eh);
	pos2 = mx + tw - bw;
	pos1 = pos2 - dd - bw;
	btnOk.MoveWindow(pos1, posy, bw, bh, TRUE);
	btnCancel.MoveWindow(pos2, posy, bw, bh, TRUE);
}

bool DlgConfig::GetMinimumSize(RECT* rectIn)
{
	if(rectIn==NULL) return false;

	rectIn->left   = 0;
	rectIn->right  = 250;
	rectIn->top    = 0;
	rectIn->bottom = 178;

	return true;
}

//---------------------------------------------------------------------------
